package com.cqjtu.store.config;

import com.cqjtu.store.interceptors.TokenHandlerInterceptor;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @ProjectName: cqjtu-store
 * @Titile: StoreWebmvcConfig
 * @Author: Lucky
 * @Description: mvc配置类
 */
@SpringBootConfiguration //组合注解
public class StoreWebmvcConfig implements WebMvcConfigurer {
    /**
     * 注册拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TokenHandlerInterceptor())
                .addPathPatterns("/user/**","/api/**") //添加；拦截url
                .excludePathPatterns("/user/login","/user/reg","/user/show",
                        "/api/category/**",
                        "/api/product/**", //放行商品模块下所有接口
                        "/api/swiper/upload" ,//放行商品模块下所有接口
                        "/api/swiper/download/**", //放行商品模块下所有接口
                        "/api/swiper/list"
                      ); //放行url
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**") // 所有请求路径
                .allowedOriginPatterns("*") // 所有来源
                .allowedHeaders("*") // 所有请求头
                .allowedMethods("*") // 所有请求方式
                .allowCredentials(true) // 允许凭证
                .maxAge(3600); // 有效期
    }
}
